package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmCluepoolService;
import com.mdd.admin.validate.ClientCrmCluepoolCreateValidate;
import com.mdd.admin.validate.ClientCrmCluepoolUpdateValidate;
import com.mdd.admin.validate.ClientCrmCluepoolSearchValidate;
import com.mdd.admin.vo.ClientCrmCluepoolListedVo;
import com.mdd.admin.vo.ClientCrmCluepoolDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmCluepool;
import com.mdd.common.mapper.ClientCrmCluepoolMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 线索池zsgc实现类
 * @author 洁洁
 */
@Service
public class ClientCrmCluepoolServiceImpl implements IClientCrmCluepoolService {
        
    @Resource
    ClientCrmCluepoolMapper clientCrmCluepoolMapper;

    /**
     * 线索池zsgc列表
     *
     * @author 洁洁
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmCluepoolListedVo>
     */
    @Override
    public PageResult<ClientCrmCluepoolListedVo> list(PageValidate pageValidate, ClientCrmCluepoolSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmCluepool> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        clientCrmCluepoolMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
            "like:number:str",
            "like:phone:str",
            "=:type:str",
            "=:status:int",
            "=:formerBusinessPersonnelId@former_business_personnel_id:int",
            "datetime:createTimeStart-createTimeEnd@create_time:str",
        });

        IPage<ClientCrmCluepool> iPage = clientCrmCluepoolMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmCluepoolListedVo> list = new LinkedList<>();
        for(ClientCrmCluepool item : iPage.getRecords()) {
            ClientCrmCluepoolListedVo vo = new ClientCrmCluepoolListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 线索池zsgc详情
     *
     * @author 洁洁
     * @param id 主键参数
     * @return ClientCrmCluepool
     */
    @Override
    public ClientCrmCluepoolDetailVo detail(Integer id) {
        ClientCrmCluepool model = clientCrmCluepoolMapper.selectOne(
                new QueryWrapper<ClientCrmCluepool>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmCluepoolDetailVo vo = new ClientCrmCluepoolDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 线索池zsgc新增
     *
     * @author 洁洁
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmCluepoolCreateValidate createValidate) {
        ClientCrmCluepool model = new ClientCrmCluepool();
        model.setContactName(createValidate.getContactName());
        model.setCustomerName(createValidate.getCustomerName());
        model.setNumber(createValidate.getNumber());
        model.setPhone(createValidate.getPhone());
        model.setType(createValidate.getType());
        model.setStatus(createValidate.getStatus());
        model.setNextFollowUpTime(createValidate.getNextFollowUpTime());
        model.setFormerBusinessPersonnelId(createValidate.getFormerBusinessPersonnelId());
        model.setFormerBusinessPersonnelName(createValidate.getFormerBusinessPersonnelName());
        model.setUserId(createValidate.getUserId());
        model.setUserName(createValidate.getUserName());
        model.setIsDelete(0);
        model.setUserId(LikeAdminThreadLocal.getAdminId());
        model.setUserName(LikeAdminThreadLocal.get("username").toString());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        model.setUpdateTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmCluepoolMapper.insert(model);
    }

    /**
     * 线索池zsgc编辑
     *
     * @author 洁洁
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmCluepoolUpdateValidate updateValidate) {
        ClientCrmCluepool model = clientCrmCluepoolMapper.selectOne(
                new QueryWrapper<ClientCrmCluepool>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setContactName(updateValidate.getContactName());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setNumber(updateValidate.getNumber());
        model.setPhone(updateValidate.getPhone());
        model.setType(updateValidate.getType());
        model.setStatus(updateValidate.getStatus());
        model.setNextFollowUpTime(updateValidate.getNextFollowUpTime());
        model.setFormerBusinessPersonnelId(updateValidate.getFormerBusinessPersonnelId());
        model.setFormerBusinessPersonnelName(updateValidate.getFormerBusinessPersonnelName());
        model.setUserId(updateValidate.getUserId());
        model.setUserName(updateValidate.getUserName());
        clientCrmCluepoolMapper.updateById(model);
    }

    /**
     * 线索池zsgc删除
     *
     * @author 洁洁
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmCluepool model = clientCrmCluepoolMapper.selectOne(
                new QueryWrapper<ClientCrmCluepool>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        clientCrmCluepoolMapper.updateById(model);
    }

}
